//
//  SAReturnGoodsListDetailCell.m
//  saasRetail
//
//  Created by 黄志刚 on 2021/7/20.
//

#import "SAReturnGoodsListDetailCell.h"
#import "SASalesInvoiceDetailModel.h"

@interface SAReturnGoodsListDetailCell ()<UITextFieldDelegate>

@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UILabel *colourSizeLab;
@property (nonatomic, strong) UILabel *returnNumLab;
@property (nonatomic, strong) UILabel *priceLab;
@property (nonatomic, strong) UILabel *totalPriceLab;

@property (nonatomic, strong) UIButton *minusBtn;
@property (nonatomic, strong) UIButton *addBtn;
@property (nonatomic, strong) UITextField *numTf;

@end

@implementation SAReturnGoodsListDetailCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initUI];
    }
    return self;
}

-(void)setModel:(SASaleSkuListModel *)model {
    _model = model;
    if (model.isReturn == YES) {
        self.addBtn.hidden = NO;
        self.minusBtn.hidden = NO;
        _titleLab.textColor = kBlackColor;
        _returnNumLab.textColor = kBlackColor;
        _totalPriceLab.textColor = kBlackColor;
        _numTf.textColor = kBlackColor;
        self.priceLab.textColor = kBlackColor;
        _numTf.userInteractionEnabled = YES;
    }else {
        self.addBtn.hidden = YES;
        self.minusBtn.hidden = YES;
        _titleLab.textColor = kLightgGrayColor;
        _returnNumLab.textColor = kLightgGrayColor;
        _totalPriceLab.textColor = kLightgGrayColor;
        _numTf.textColor = kLightgGrayColor;
        self.priceLab.textColor = kLightgGrayColor;
        _numTf.userInteractionEnabled = NO;
    }
    
    self.titleLab.text = [NSString stringWithFormat:@"%@,%@",model.colourName,model.sizeName];
    self.returnNumLab.text = [NSString stringWithFormat:@"%zd",[model.count integerValue] - [model.returnCount integerValue]];
    self.priceLab.text = [NSString stringWithFormat:@"%0.2lf",[model.price doubleValue]/100.f];
    self.totalPriceLab.text = [NSString stringWithFormat:@"%0.2lf",[model.skuActualPrice doubleValue]/100.f];
    self.numTf.text = model.stock;

}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (string.length == 0) {
        return YES;
    }
    if (self.numTf == textField ) {
        if (textField.text.length > 5) {
            return NO;
        }
    }
    NSString *newStr = [NSString stringWithFormat:@"%@%@",textField.text,string];
    NSInteger num = [newStr integerValue];
    NSInteger returnCount = [self.model.count integerValue] - [self.model.returnCount integerValue];
    if (num > returnCount) {
        return NO;
    }
    return [self validateNumber:string];
}

- (void)textFieldEditChanged:(UITextField *)textField {
    if (textField.text.length == 0) {
        textField.text = @"";
        self.model.stock = @"";
        !self.inputBlock ?: self.inputBlock(@"");
    }else {
        NSInteger num = [textField.text integerValue];
        NSInteger returnCount = [self.model.count integerValue] - [self.model.returnCount integerValue];
        if (num >= returnCount) {
            num = returnCount;
        }
        textField.text = [NSString stringWithFormat:@"%zd",num];
        self.model.stock = [NSString stringWithFormat:@"%zd",num];
        !self.inputBlock ?: self.inputBlock(self.model.stock);
    }
}

- (BOOL)validateNumber:(NSString*)number {
    BOOL res = YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    int i = 0;
    while (i < number.length) {
        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            res = NO;
            break;
        }
        i++;
    }
    return res;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [self endEditing:YES];
    return YES;
}

#pragma mark - event
-(void)clickedMinusBtn {
    NSInteger num = [self.model.stock integerValue] - 1;
    if (num <= 0) {
        num = 0;
    }
    self.numTf.text = [NSString stringWithFormat:@"%zd",num];
    self.model.stock = [NSString stringWithFormat:@"%zd",num];
    !self.cutBlock ?: self.cutBlock();
}

-(void)clickedAddBtn {
    NSInteger num = [self.model.stock integerValue] + 1;
    NSInteger returnCount = [self.model.count integerValue] - [self.model.returnCount integerValue];
    if (num >= returnCount) {
        num = returnCount;
    }
    self.numTf.text = [NSString stringWithFormat:@"%zd",num];
    self.model.stock = [NSString stringWithFormat:@"%zd",num];
    !self.addBlock ?: self.addBlock();
}

-(void)initUI {
    [self.contentView addSubview:self.titleLab];
    [self.contentView addSubview:self.colourSizeLab];
    [self.contentView addSubview:self.returnNumLab];
    [self.contentView addSubview:self.priceLab];
    [self.contentView addSubview:self.totalPriceLab];
    
    [self.contentView addSubview:self.numTf];
    [self.contentView addSubview:self.minusBtn];
    [self.contentView addSubview:self.addBtn];
}

-(UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] initWithFrame:SARectMake(20, 0, 75, 40)];
        _titleLab.textColor = kBlackColor;
        _titleLab.font = kFont(13);
    }
    return _titleLab;
}

-(UILabel *)returnNumLab {
    if (!_returnNumLab) {
        _returnNumLab = [[UILabel alloc] initWithFrame:SARectMake(100, 0, 60, 40)];
        _returnNumLab.textColor = kBlackColor;
        _returnNumLab.font = kFont(13);
        _returnNumLab.textAlignment = NSTextAlignmentCenter;
    }
    return _returnNumLab;
}

-(UILabel *)priceLab {
    if (!_priceLab) {
        _priceLab = [[UILabel alloc] initWithFrame:SARectMake(160, 0, 55, 40)];
        _priceLab.textColor = kBlackColor;
        _priceLab.font = kFont(13);
        _priceLab.textAlignment = NSTextAlignmentCenter;
    }
    return _priceLab;
}


-(UILabel *)totalPriceLab {
    if (!_totalPriceLab) {
        _totalPriceLab = [[UILabel alloc] initWithFrame:SARectMake(215, 0, 55, 40)];
        _totalPriceLab.textColor = kBlackColor;
        _totalPriceLab.font = kFont(13);
        _totalPriceLab.textAlignment = NSTextAlignmentCenter;
    }
    return _totalPriceLab;
}


-(UITextField *)numTf {
    if (!_numTf) {
        _numTf = [[UITextField alloc] initWithFrame:SARectMake(300, 0, 45, 40)];
        _numTf.textColor = kBlackColor;
        _numTf.textAlignment = NSTextAlignmentCenter;
        _numTf.font = kFont(13);
        _numTf.delegate = self;
        _numTf.keyboardType = UIKeyboardTypeNumberPad;
        _numTf.returnKeyType = UIReturnKeyDone;
        [_numTf addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _numTf;
}

-(UIButton *)minusBtn {
    if (!_minusBtn) {
        _minusBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_minusBtn setImage:[UIImage imageNamed:@"commodity_minus"] forState:UIControlStateNormal];
        _minusBtn.frame = SARectMake(270, 0, 40, 40);
        _minusBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        [_minusBtn addTarget:self action:@selector(clickedMinusBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _minusBtn;
}

-(UIButton *)addBtn {
    if (!_addBtn) {
        _addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_addBtn setImage:[UIImage imageNamed:@"commodity_add3"] forState:UIControlStateNormal];
        _addBtn.frame = SARectMake(335, 0, 40, 40);
        _addBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        [_addBtn addTarget:self action:@selector(clickedAddBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addBtn;
}

//if (i == 0) {
//    titleLab.frame = CGRectMake(kScaleNum(20), 0, kScaleNum(75), kScaleNum(30));
//    titleLab.textAlignment = NSTextAlignmentLeft;
//}else if (i == 1) {
//    titleLab.frame = CGRectMake(kScaleNum(100), 0, kScaleNum(60), kScaleNum(30));
//}else if (i == 2) {
//    titleLab.frame = CGRectMake(kScaleNum(160), 0, kScaleNum(55), kScaleNum(30));
//}else if (i == 3) {
//    titleLab.frame = CGRectMake(kScaleNum(215), 0, kScaleNum(55), kScaleNum(30));
//}else if (i == 4) {
//    titleLab.frame = CGRectMake(kScaleNum(270), 0, kScaleNum(105), kScaleNum(30));
//}

@end
